package com.hxzy.projectssmplus.controller;

import com.hxzy.projectssmplus.entity.SysMenu;
import com.hxzy.projectssmplus.service.SysMenuService;
import com.hxzy.projectssmplus.vo.AjaxResult;
import com.hxzy.projectssmplus.vo.RoleTreeVO;
import com.hxzy.projectssmplus.vo.TreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2021/11/30 20:23
 * Description:
 * Version: V1.0
 */
@RestController
@RequestMapping(value = "/system/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 获取菜单列表
     * @param sysMenu
     * @return
     */
    @PreAuthorize("hasAuthority('system:menu:list')")
    @GetMapping(value = "/list")
    public AjaxResult list(SysMenu sysMenu){
        List<SysMenu> sysMenus = this.sysMenuService.selectMenuList(sysMenu);
        return AjaxResult.success_data(sysMenus);
    }

    /**
     *根据菜单编号获取详细信息
     */
    @PreAuthorize("hasAnyAuthority('system:menu:query')")
    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable Long menuId){
        SysMenu byId = this.sysMenuService.getById(menuId);
        return AjaxResult.success_data(byId);
    }

    /**
     * 获取下拉树列表
     */
    @GetMapping(value = "/treeSelect")
    public AjaxResult treeselect(SysMenu sysMenu){
        List<TreeVO> treeVOList = this.sysMenuService.GetTreeSelect(sysMenu);

        return AjaxResult.success_data(treeVOList);
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeSelect")
    public AjaxResult roleMenuTreeSelect (Long roleId){
        List<RoleTreeVO> treeByRoleId = this.sysMenuService.getTreeByRoleId(roleId);
        return AjaxResult.success_data(treeByRoleId);
    }

    /**
     * 新增菜单
     */
    @PreAuthorize("hasAnyAuthority('system:menu:add')")
    @PostMapping(value = "/add")
    public AjaxResult add(@Validated @RequestBody SysMenu sysMenu){
        List<SysMenu> menu_name = this.sysMenuService.query().eq("menu_name", sysMenu.getMenuName()).list();
        if(menu_name.size()>0){
            return AjaxResult.error("菜单名称已存在");
        }
        sysMenu.setCreateBy("admin");
        sysMenu.setCreateTime(new Date());
        boolean save = this.sysMenuService.save(sysMenu);
        return save?AjaxResult.success("插入成功"):AjaxResult.error("信息有误，插入失败");
    }

    /**
     * 修改菜单
     */
    @PreAuthorize("hasAuthority('system:menu:edit')")
    @PutMapping(value = "/update")
    public AjaxResult edit(@Validated @RequestBody SysMenu sysMenu){
        List<SysMenu> menu_name = this.sysMenuService.query().eq("menu_name", sysMenu.getMenuName()).notIn("menu_id",sysMenu.getMenuId()).list();
        if(menu_name.size()>0){
            return AjaxResult.error("菜单名称已存在");
        }
        sysMenu.setUpdateBy("admin");
        sysMenu.setUpdateTime(new Date());
        boolean updateById = this.sysMenuService.updateById(sysMenu);
        return updateById?AjaxResult.success("修改成功"):AjaxResult.error("信息有误，修改失败");
    }

    /**
     * 删除菜单
     */
    @PreAuthorize("hasAuthority('system:menu:remove')")
    @DeleteMapping(value = "/{menuId}")
    public AjaxResult remove(@PathVariable("menuId") Long menuId){
        List<SysMenu> parent_id = this.sysMenuService.query().eq("parent_id", menuId).list();
        if (parent_id.size()>0){
            return AjaxResult.error("有子级菜单，不允许删除");
        }
        boolean removeById = this.sysMenuService.removeById(menuId);
        return removeById?AjaxResult.success("删除成功"):AjaxResult.error("信息有误，删除失败");
    }
}
